<?php

namespace App\Admin\Controllers;

use App\model\SendNoticeUser;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Spatie\Permission\Models\Role;

class SendNoticeUserController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('小程序模板配置')
            ->description('')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑模板')
            ->description('')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('创建模板')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SendNoticeUser);


        $grid->type('模板');
        $grid->role('发送角色')->display(function($value){
            if (empty($value)){
                return '未设置';
            }
           $role= Role::whereIn('id',$value)->get();

            $name='';
            foreach ($role as $v){
                $name.="<span class='label label-success'>$v->name</span>&nbsp";
            }
            return $name;
        });
        $grid->disableCreateButton();
        $grid->actions(function (Grid\Displayers\Actions $actions){
            $actions->disableView();
            $actions->disableDelete();
        });


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(SendNoticeUser::findOrFail($id));

        $show->id('Id');
        $show->type('Type');
        $show->role('Role');
        $show->crearted_at('Crearted at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SendNoticeUser);



        $form->multipleSelect('role','角色')->options(Role::pluck('name','id'));


        return $form;
    }
}
